/* 
 *  <copyright file="GuidGameService.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;

namespace SobrietyEngine.Guids
{
    /// <summary>
    /// Handles transparently assigning guids to the CGuid class, no need to call anything here manually
    /// </summary>
    public class GuidGameService:GameComponent
    {
        //singleton
        static private GuidGameService Singleton;        

        public GuidGameService(Game game):base(game)
        {            
            Singleton = this;
        }

        public static GuidGameService GetSingleton()
        {
            if (Singleton == null)
                throw new Exception("Singleton is null");
            return Singleton;
        }

        //guid stuff
        Dictionary<CGuid, Object> GuidToObject = new Dictionary<CGuid,object>();
        Dictionary<Object, CGuid> ObjectToGuid = new Dictionary<object, CGuid>();

        //guid name stuff
        Dictionary<CGuid, String> GuidToName = new Dictionary<CGuid,string>();
        Dictionary<String, CGuid> NameToGuid = new Dictionary<string,CGuid>();

        //net guid value
        static Int32 nextGuid = 0;

        //pool
        Queue<CGuid> guidPool = new Queue<CGuid>();

        public CGuid CreateEmptyGuid()
        {
            CGuid guid;
            if (guidPool.Count > 0)
                guid = guidPool.Dequeue();
            else
                guid = new CGuid();
            guid.Clear();

            return guid;
        }

        public CGuid CreateGuid(Object obj)
        { 
            CGuid guid;
            if (guidPool.Count > 0)
                guid = guidPool.Dequeue();
            else
                guid = new CGuid();

            guid.Set(++nextGuid, String.Empty, false);
            GuidToObject.Add(guid, obj);

            if(obj!=null)
                ObjectToGuid.Add(obj, guid);

            return guid;
        }

        public CGuid CreateGuid(String name, Object obj)
        {
            CGuid guid;
            if (guidPool.Count > 0)
                guid = guidPool.Dequeue();
            else
                guid = new CGuid();

            guid.Set(++nextGuid, name, true);

            GuidToObject.Add(guid, obj);

            if(obj!=null)
                ObjectToGuid.Add(obj, guid);

            CreateNameForGuid(guid, name);

            return guid;
        }

        public void CreateNameForGuid(CGuid guid, String name)
        {
            guid.Set(guid.Value, name, true);

            NameToGuid.Add(name, guid);
            GuidToName.Add(guid, name);
        }

        public CGuid GetObjectGuid(Object obj)
        {
            CGuid guid = null;
            if (ObjectToGuid.TryGetValue(obj, out guid))
                return guid;
            else return null;
        }

        public Object GetGuidObject(CGuid guid)
        {
            object obj = null;
            if (GuidToObject.TryGetValue(guid, out obj))
                return obj;
            else return null;
        }

        public CGuid GetGuidFromName(string guidName)
        {            
            CGuid obj = null;
            if (NameToGuid.TryGetValue(guidName, out obj))
                return obj;
            else return null;
        }

        public void RemoveGuid(CGuid guid)
        {
            Object target = guid.Resolve();
            GuidToObject.Remove(guid);

            if(target!=null)
                ObjectToGuid.Remove(target);

            if (guid.HasName)
            {
                NameToGuid.Remove(guid.Name);
                GuidToName.Remove(guid);
            }

            guid.Clear();
            
            //add to pool
            guidPool.Enqueue(guid);
        }

        public void Clear()
        {
            //fill the pool
            foreach (CGuid guid in GuidToObject.Keys)
            {
                guid.Clear();
                guidPool.Enqueue(guid);
            }

            //clear the dictionaries
            GuidToObject.Clear();
            ObjectToGuid.Clear();

            NameToGuid.Clear();
            GuidToName.Clear();            
        }
    }
}
